package mysql

import (
	"backend/models"
	"backend/utils"
	"database/sql"
	"fmt"
)

// GetOperateHistoryList 获取操作历史列表
func GetOperateHistoryList(whereArgsList ...map[string]string) (operateHistoryList []*models.OperateHistory, err error) {
	// Where条件拼接
	whereSql := ""
	if len(whereArgsList) != 0 {
		whereSql = utils.ParamFormat(whereArgsList[0], []string{"limit", "offset"})
	}
	sqlStr := fmt.Sprintf(`SELECT id,create_time,update_time,pnode_id,content FROM operate_history %s`, whereSql)
	err = db.Select(&operateHistoryList, sqlStr)
	if err == sql.ErrNoRows {
		err = nil
		return
	}
	return
}

// GetOperateHistoryById 获取操作历史详情
func GetOperateHistoryById(id int) (operateHistory models.OperateHistory, err error) {
	sqlStr := `SELECT id,create_time,update_time,pnode_id,content FROM operate_history WHERE id=?`
	err = db.Get(&operateHistory, sqlStr, id)
	if err != nil {
		return
	}
	return
}

// DelOperateHistoryById 删除操作历史记录
func DelOperateHistoryById(id int) (operateHistory models.OperateHistory, err error) {
	sqlStr := `DELETE FROM operate_history WHERE id=?`
	_, err = db.Exec(sqlStr, id)
	if err != nil {
		return
	}
	return
}
